﻿using System;
using AutoTaskSchedule.Core.Models;
using FluentScheduler;

namespace AutoTaskSchedule.Core.Business
{
    public class IntervalTaskFactory : TaskFactory
    {
        public IntervalTaskFactory(Task task)
            : base(task)
        {
        }

        public override void Create()
        {
            if (!_task.Interval.HasValue) return;

            Action<Schedule> action =
                shchedule =>
                    shchedule.WithName(_task.Name).NonReentrant().ToRunNow().AndEvery(_task.Interval.Value).Seconds();

            var autoTaskSchedule = GetAutoTaskSchedule();
            JobManager.AddJob(() => autoTaskSchedule.Execute(), shchedule => action.Invoke(shchedule));
        }
    }
}